(function(factory) {
	if (typeof define === "function" && define.amd) {
		// AMD. Register as an anonymous module.
		define(factory);
	} else {
		// Browser globals
		factory();
	}
}(function() {
	/**
	 * @author 李球 <service@hteos.com>
	 * @class HteOS.dock.Dock 侧边栏组件
	 * @constructor
	 */
	var Dock = HteOS.dock.Dock = function(app) {
		var dock = this;
		var $el = $("<div class=\"col-md-3 col-xs-12 col-sm-6 hte-dock hte-side hte-dock-animate "+(HteOS.UA.CSS3Transform ? "hte-dock-transform" : "hte-dock-notransform")+"\">" + 
			"<div class=\"hte-dock-inner\">" +
				"<header class=\"hte-dock-header\">" +
					"<h3><span>" + (app.name || "") + "</span>" +
					"<span class=\"metro-icon-arrow-right-3 smaller hte-dock-close-btn\"></span>" +
					"</h3>" +
				"</header>" +
				"<div class=\"hte-dock-body\">" +
				"</div>" +
			"</div>" +
		"</div>");
		this.id = "hte-dock-" + app.id;
		this.el = $el;
		$el.attr("id",this.id);
		$el.appendTo(HteOS.getContainer());
		app.dockId = this.id;
		HteOS.apply(this,{
			path : app.path,
			loadType : app.loadType,
			showed :false,
			html : app.html
		});
		HteOS.dock.DockManager.register(this);
	};
	Dock.prototype.getEl = function(){
		return this.el;
	};

	/**
	 * 加载远程内容
	 * */
	Dock.prototype.load = function() {
		var me = this,
			body = me.getEl().find(".hte-dock-body");
		HteOS.Masker.mask(body,{
			modal: false,
			msg: '正在加载...'
		});
		if(this.html){
			body.html(this.html);
		}else{ 
			if(this.loadType == 'iframe'){
				var iframe = $("<iframe frameborder=\"0\" src=\""+this.path+"\"></iframe>" );
				iframe.appendTo(body);
				// iframe加载后更改状态
				iframe.load(function() {
					HteOS.Masker.unmask(body);
					me.loaded = true;
				});
			}else{
				$.ajax({
					url: me.path,
					dataType: 'html'
				}).success(function(html) {
					HteOS.Masker.unmask(body);
					body.html(html);
				}).error(function(){
					HteOS.Masker.mask(body,{
						modal: false,
						loading : false,
						msg: '加载资源失败：' + me.path
					});
				}).complete(function() {
					//HteOS.Masker.unmask(body);
					me.loaded = true;
				});
			}
		}
	};
	
	Dock.prototype.move = function(position){
		if(HteOS.UA.CSS3Transform){
			this.el.css("transform","translate3d("+position+"px,0px,0px)");
		}else{
			this.el.css('right', 0 - position);
		}
	};

	/**
	 *显示侧边栏
	 */
	Dock.prototype.show = function() {
		if(this.showed === true){
			return;
		}
		//TODO 建立事件机制
		var dock = this,$el = this.getEl();
		// 隐藏其他的侧边栏
		HteOS.EventManager.trigger("hte.dock.beforeshow");
		$el.show();
		//if (!$.is.advance) {
		window.setTimeout(function(){
			dock.move(0,"show");
		},100);
		dock.showed = true;
		if (dock.path && !dock.loaded) {
			dock.load();
		}
		/*} else {
			dock.$el.animate({
						right : 0
					}, 'fast', null, function() {
						dock.showed = true;
						if (dock.path && !dock.loaded) {
							dock.load();
						}
					});
		}*/
		$el.css('z-index', 9999);
		HteOS.EventManager.trigger("hte.dock.show",this);
	};

	/**
	 * 隐藏侧边栏
	 * */
	Dock.prototype.hide = function() {
		if(this.showed === false){
			return ;
		}
		var dock = this,$el = this.getEl();
		$el.css("z-index", 9998);
		//if (!$.is.advance) {
		this.move($el.outerWidth());
		dock.showed = false;
		/*} else {
			dock.$el.animate({
						right : 0 - this.$el.outerWidth()
					}, 'fast', null, function() {
						dock.showed = false;
						dock.$el.hide();
					});
		}*/
		window.setTimeout(function(){
			$el.hide();
		},300);
		HteOS.EventManager.trigger("hte.dock.hide");
	};

	/**
	 * 切换侧边栏
	 * */
	Dock.prototype.toggle = function() {
		if (this.showed === true) {
			this.hide();
		} else {
			this.show();
		}
	}
}));